/*******************************************************************************
 * ATE, Automation Test Engine
 *
 * Copyright 2015, Montreal PROT, or individual contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Montreal PROT.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
 * The Class LoginForm.
 */
public class LoginForm {
	
	/** The driver. */
	private final WebDriver driver;
	/** The loginform locator. */
	private By loginformLocator = By.id("form-login");
    
    /** The username locator. */
    private By usernameLocator = By.id("modlgn_username");
    
    /** The password locator. */
    private By passwordLocator = By.id("modlgn_passwd");
    
    /** The login button locator. */
    private By loginButtonLocator = By.name("Submit");//NOPMD
    
	/**
	 * @return the loginformLocator
	 */
	public By getLoginformLocator() {
		return loginformLocator;
	}

	/**
	 * @param loginformLocator the loginformLocator to set
	 */
	public void setLoginformLocator(final By loginformLocator) {
		this.loginformLocator = loginformLocator;
	}

	/**
	 * @return the usernameLocator
	 */
	public By getUsernameLocator() {
		return usernameLocator;
	}

	/**
	 * @param usernameLocator the usernameLocator to set
	 */
	public void setUsernameLocator(final By usernameLocator) {
		this.usernameLocator = usernameLocator;
	}

	/**
	 * @return the passwordLocator
	 */
	public By getPasswordLocator() {
		return passwordLocator;
	}

	/**
	 * @param passwordLocator the passwordLocator to set
	 */
	public void setPasswordLocator(final By passwordLocator) {
		this.passwordLocator = passwordLocator;
	}

	/**
	 * @return the loginButtonLocator
	 */
	public By getLoginButtonLocator() {
		return loginButtonLocator;
	}

	/**
	 * @param loginButtonLocator the loginButtonLocator to set
	 */
	public void setLoginButtonLocator(final By loginButtonLocator) {//NOPMD
		this.loginButtonLocator = loginButtonLocator;
	}

	/**
	 * @return the driver
	 */
	public WebDriver getDriver() {
		return driver;
	}

	
    /**
     * Instantiates a new login form.
     *
     * @param driver the driver
     */
    public LoginForm(final WebDriver driver) {
        this.driver = driver;
        
        if (!driver.findElement(loginformLocator).isDisplayed()) {
            throw new IllegalStateException("There is no login form");
        }
    }
    
    /**
     * Type username.
     *
     * @param username the username
     * @return the login form
     */
    public LoginForm typeUsername(final String username) {
        
        driver.findElement(usernameLocator).sendKeys(username);

        return this;    
    }
    
    /**
     * Type password.
     *
     * @param password the password
     * @return the login form
     */
    public LoginForm typePassword(final String password) {
        
        driver.findElement(passwordLocator).sendKeys(password);
        return this;    
    }
    
    /**
     * Submit login.
     *
     * @return the success login page
     */
    public SuccessLoginPage submitLogin() {
        
        driver.findElement(loginButtonLocator).submit();
        return new SuccessLoginPage(driver);    
    }
    
    /**
     * Submit false login.
     *
     * @return the false login page
     */
    public FalseLoginPage submitFalseLogin() {
       
        driver.findElement(loginButtonLocator).submit();
        return new FalseLoginPage(driver);   
    }

    /**
     * Login as.
     *
     * @param username the username
     * @param password the password
     * @return the success login page
     */
    public SuccessLoginPage loginAs(final String username, final String password) {
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
}
